﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class ClassConfig
    {
        //public enum License { Guest, User, Checker, Director, Issuer, Archive };

        public ClassConfig()
        {
            OutPath = new List<string>();
        }

        public void ToDefault()
        {
            //this.UserGuid = MainWindow.instance.config.UserGuid;
            State = true;
            UserName = null;
            UserLicense = License.Guest;
            IsServer = false;
            IsAdmin = false;
            IsCustomize = false;
            IsQueryOnly = false;
            InPath = "E:\\收到的文件\\";
            OutPath = new List<string>();
            OutPath.Add("E:\\");
        }

        public void NewUser(string name)
        {
            ToDefault();
            UserGuid = Guid.NewGuid();
            UserName = name;
        }
        
        /// <summary>
        /// 用户唯一性识别代码
        /// </summary>
        public Guid UserGuid
        {
            set;
            get;
        }

        /// <summary>
        /// 用户在职/离职状态
        /// </summary>
        public bool State
        {
            set;
            get;
        }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName
        {
            set;
            get;
        }

        /// <summary>
        /// 用户权限
        /// </summary>
        public License UserLicense
        {
            set;
            get;
        }

        /// <summary>
        /// 是否服务器
        /// </summary>
        public bool IsServer
        {
            set;
            get;
        }

        /// <summary>
        /// 是否管理员
        /// </summary>
        public bool IsAdmin
        {
            set;
            get;
        }

        /// <summary>
        /// 用户是否可以自定义用户权限、管理员、查询模式
        /// </summary>
        public bool IsCustomize
        {
            set;
            get;
        }

        /// <summary>
        /// 是否仅查询界面
        /// </summary>
        public bool IsQueryOnly
        {
            set;
            get;
        }

        /// <summary>
        /// 接收文件的路径
        /// </summary>
        public string InPath
        {
            set;
            get;
        }

        /// <summary>
        /// 发送文件、报告文件储存的路径
        /// </summary>
        public List<string> OutPath
        {
            set;
            get;
        }

        public string OutPath2String()
        {
            List<string> list = this.OutPath;
            string str = null;
            foreach (string s in list)
            {
                str += s + ",";
            }
            return str;
        }

        public void OutPathFromString(string message)
        {
            string[] str = message.Split(',');
            foreach(string buff in str)
            {
                if (!buff.Equals(""))
                {
                    this.OutPath.Add(buff);
                }
            }
        }

        public override bool Equals(object obj)
        {
            var other = obj as ClassConfig;
            return Equals(other);
        }

        public bool Equals(ClassConfig other)
        {
            return other != null && Coding2String() == other.Coding2String();
        }

        public override int GetHashCode()
        {
            return Coding2String().GetHashCode();
        }

        public static bool operator ==(ClassConfig configL, ClassConfig configR)
        {
            if(ReferenceEquals(configL, configR))
            {
                return true;
            }
            if((object)configL == null || (object)configR == null)
            {
                return false;
            }
            return configL.Equals(configR);
        }

        public static bool operator !=(ClassConfig configL, ClassConfig configR)
        {
            return !(configL == configR);
        }

        public void Copy(ClassConfig other)
        {
            this.InPath = other.InPath;
            this.IsAdmin = other.IsAdmin;
            this.IsCustomize = other.IsCustomize;
            this.IsQueryOnly = other.IsQueryOnly;
            this.IsServer = other.IsServer;
            this.OutPath = other.OutPath;
            this.State = other.State;
            this.UserGuid = other.UserGuid;
            this.UserLicense = other.UserLicense;
            this.UserName = other.UserName;
        }

        public string Coding2String()
        {
            string b = null;
            b += this.UserGuid.ToString() + "♠";
            b += this.State.ToString() + "♠";
            b += this.UserName + "♠";
            b += this.UserLicense + "♠";
            b += this.IsServer + "♠";
            b += this.IsAdmin + "♠";
            b += this.IsCustomize + "♠";
            b += this.IsQueryOnly + "♠"; 
            b += this.InPath + "♠";
            b += this.OutPath2String();
            return b;
        }

        public void DecodingFromString(string message)
        {
            string[] str = message.Split('♠');
            this.UserGuid = new Guid(str[0]);
            this.State = Boolean.Parse(str[1]);
            this.UserName = str[2];
            this.UserLicense = (License)Enum.Parse(typeof(License), str[3]);
            this.IsServer = Convert.ToBoolean(str[4]);
            this.IsAdmin = Convert.ToBoolean(str[5]);
            this.IsCustomize = Convert.ToBoolean(str[6]);
            this.IsQueryOnly = Convert.ToBoolean(str[7]);
            this.InPath = str[8];
            this.OutPathFromString(str[9]);
        }
    }
}
